package com.llmcu.source.javase.thread.threadlocal;

import java.util.concurrent.TimeUnit;

/**
 * 多个ThreadLocal对象
 *
 * @author liuling
 * @date 2024/11/28 19:23
 */
public class MultiThreadLocalDemo {
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    private static ThreadLocal<String> threadLocal2 = new ThreadLocal<>();
    public static void main(String[] args) {
        new Thread(()->{
            threadLocal.set("A");
            threadLocal2.set("AA");
            System.out.println(Thread.currentThread().getId()+"-set:A");
            System.out.println(Thread.currentThread().getId()+"-set2:AA");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getId()+"-get:"+threadLocal.get());
            System.out.println(Thread.currentThread().getId()+"-get2:"+threadLocal2.get());
            threadLocal.remove();
            threadLocal2.remove();
        }).start();
        new Thread(()->{
            threadLocal.set("B");
            threadLocal2.set("BB");
            System.out.println(Thread.currentThread().getId()+"-set:B");
            System.out.println(Thread.currentThread().getId()+"-set2:BB");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getId()+"-get:"+threadLocal.get());
            System.out.println(Thread.currentThread().getId()+"-get2:"+threadLocal2.get());
            threadLocal.remove();
            threadLocal2.remove();
        }).start();
    }
}
